package cc.vv.baselibrary.util;

import android.app.ActivityManager;
import android.content.Context;

import java.util.List;

/**
 * app管理工具类. 包括功能:退出登录,清空缓存等与app相关的业务功能
 *
 * @author dupeng dupeng@vv.cc
 * @date 2018/4/12
 */

public class AppManager {

    private static AppManager mInstance;

    private AppManager() {
    }

    public static AppManager getInstance() {
        if (null == mInstance) {
            synchronized (AppManager.class) {
                if (null == mInstance) {
                    mInstance = new AppManager();
                }
            }
        }
        return mInstance;
    }


    /**
     * 退出登录
     *
     * @param context
     */
    public void loginOut(final Context context) {
        final Context fContext = context;
    }

    /**
     * 判断服务是否启动,context上下文对象 ，className服务的name
     */
    public boolean isServiceRunning(Context mContext, String className) {
        boolean isRunning = false;
        ActivityManager activityManager = (ActivityManager) mContext
                .getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningServiceInfo> serviceList = activityManager
                .getRunningServices(30);

        if (!(serviceList.size() > 0)) {
            return false;
        }

        for (int i = 0; i < serviceList.size(); i++) {
            if (serviceList.get(i).service.getClassName().equals(className) == true) {
                isRunning = true;
                break;
            }
        }
        return isRunning;
    }

    /**
     * 清空app缓存.这个方法还需完善!!!
     *
     * @param context
     * @param clearCacheCallBack
     */
    public void clearAppCache(Context context, final ClearCacheCallBack clearCacheCallBack) {
    }

    public interface ClearCacheCallBack {
        void onSuccess();
    }

}
